package com.service.single.service.impl;

import com.alibaba.fastjson.JSON;
import com.service.single.constant.EsIndex;
import com.service.single.entity.FileContent;
import com.service.single.entity.FileInfo;
import com.service.single.service.ElasticsearchService;
import com.service.single.util.R;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Author: 一休
 * @DateTime: 2022/11/4 15:55
 */
@Slf4j
@Service
public class ElasticsearchServiceImpl implements ElasticsearchService {


    @Resource
    private RestHighLevelClient client;

    /**
     * 创建 索引
     *
     * @param index
     */
    @Override
    public R<String> createdIndex(String index) {
        GetIndexRequest getIndexRequest = new GetIndexRequest(index);
        try {
            boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
            if (exists) {
                return R.faild("索引已存在");
            }
            CreateIndexRequest request = new CreateIndexRequest(index);
            CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
            boolean isCreate = response.isAcknowledged();
            if (isCreate) {
                return R.success("创建成功");
            }
        } catch (Exception e) {
            log.error("创建索引异常 {}", e);
        }
        return R.faild("创建失败");
    }

    /**
     * 添加文件内容
     *
     * @param fileInfo 上传文件的信息
     * @param builder  文件内容
     */
    @Override
    public void insertFileContent(FileInfo fileInfo, StringBuilder builder) {
        try {
            FileContent content = new FileContent();
            content.setId(fileInfo.getId());
            content.setTitle(fileInfo.getFileName());
            content.setContent(builder.toString());
            content.setCreatedTime(LocalDateTime.now());
            // 添加到 es
            IndexRequest request = new IndexRequest(EsIndex.FILE);
            request.id(String.valueOf(content.getId()));
            request.source(JSON.toJSONString(content), XContentType.JSON);
            client.index(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("文件内容添加异常 {}", e);
        }
    }

    /**
     * 根据es id 删除文档
     *
     * @param id
     */
    @Override
    public void deleteFileContent(Long id) {
        try {
            DeleteRequest request = new DeleteRequest(EsIndex.FILE);
            request.id(String.valueOf(id));
            client.delete(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("删除文档异常 {}", e);
        }
    }
}
